package asteroids;

import java.awt.Point;
import java.util.List;

import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.GameScene;
import com.uqbar.vainilla.colissions.CollisionDetector;

public class AsteroidsComponent extends GameComponent<GameScene>{
	
	private Point vector = new Point(0,0);
	
	public AsteroidsComponent(double x,double y) {
		super(x,y);
	}
	
	public void checkScreenLimits(){
		int winWidth = this.getGame().getDisplayWidth();
		int winHeight = this.getGame().getDisplayHeight();
		double x = this.getX();
		double y = this.getY();
		if(x>winWidth)
			this.setX(-this.getWidth());
		if(x<-this.getWidth())
			this.setX(winWidth);
		if(y>winHeight)
			this.setY(-this.getHeight());
		if(y<-this.getHeight())
			this.setY(winHeight);
	}
	
	//CHEQUEO DE COLISIONES
	public boolean collides(AsteroidsComponent a){
		//parche a las colisiones: para que colisione un poco mejor le sumo 5 a X y a Y
		//y le resto 15 al width y al heigth		
		return CollisionDetector.INSTANCE.collidesRectAgainstRect(this.getX()+5,this.getY()+5,
				this.getWidth()-15,this.getHeight()-15,
				a.getX()+5,a.getY()+5,
				a.getWidth()-15,a.getHeight()-15);
	}
	
	public boolean collidesAgainstAsteroid() {
		boolean collides = false;
		AsteroidsGame game = this.getGame();
		List<Asteroid> asteroids = game.getAsteroids();
		
		Asteroid shootedAsteroid = null;
		for(Asteroid asteroid : asteroids){
			if(this.collides(asteroid)){
				shootedAsteroid = asteroid;
				collides = true;
			}
		}
		if(collides){
			shootedAsteroid.shooted();
			asteroids.remove(shootedAsteroid);			
		}			
		
		return collides;
	}

	public int getWidth() {
		return (int)this.getAppearance().getWidth();
	}

	public int getHeight() {
		return (int)this.getAppearance().getHeight();
	}
	
	public Point getVector() {
		return vector;
	}

	public void setVector(Point vector) {
		this.vector = vector;
	}

	@Override
	public AsteroidsGame getGame() {		
		return (AsteroidsGame)super.getGame();
	}
	
	public Level getLevel(){
		return (Level)super.getGame().getCurrentScene();
	}	
	

}
